# The scheduler is the brains of the controller. It decides which  
# projects should update and in which order.
#
# If you are looking for the place to change the business logic for 
# site generation priority, this is the place. 

require 'singleton'
require 'monitor'
class Scheduler < Monitor
  include Singleton
  
  ## This is the brains of the controller 
  #   when a generator asks for work this is where 
  #   the decision is made.
  #    
  #   The only place that calls this code is generations_controller.rb#create
  def next_project
    Project.find(:first, :order => 'id ASC')
  end
  
  ## temp work on a smarter conroller
  ##
  ## WARNING: this method is not synchronized, so if you're assigning jobs
  ## based on this data, make sure 
  ## you don't dole out the same site to two generatos
  def better_next_project(logger)
    
      ## first get all of the active projects
      active_projects = Project.find(:all, :conditions => {:active => true} )
      logger.info "Picking Projects Choices: " + active_projects.length.to_s

      
      ## first add the weekly ones
      weekly_projects = Array.new
      daily_projects = Array.new
      constantly_projects = Array.new
      
      active_projects.each do |project|
        ## see if the project should generate
        if project.should_generate?
          case project.site_update_frequency.generator_name
          when 'weekly'
            ## the gerator is starting
            logger.info "Adding weekly project " + project.name
            weekly_projects.push(project)
          when 'daily'
            logger.info "Adding daily project " + project.name
            daily_projects.push(project)
          else
            logger.info "Adding constantly project " + project.name
            constantly_projects.push(project)
          end
        end
      end
      
      ## give them a weekly project if you have one
      if !weekly_projects.empty?
        ##sort them and get going
        weekly_projects.sort! {|x,y| x.latest_complete_generation_time <=> y.latest_complete_generation_time }
        logger.info "RETURNING WEEKLY PROJECT"
        return weekly_projects[0]
      end
    
      ## give them a daily project if you have one
      if !daily_projects.empty?
        ##sort them and get going
        daily_projects.sort! {|x,y| x.latest_complete_generation_time <=> y.latest_complete_generation_time }
        logger.info "RETURNING DAILY PROJECT"
        return daily_projects[0]
      end
      
      ## and if there was no weekly or daily projects, do a constantly one
      if !constantly_projects.empty?
        ##sort them and get going
        constantly_projects.each do |project|
          logger.info project.name + " :: " + project.latest_complete_generation_time.to_s
        end
        constantly_projects.sort! {|x,y| x.latest_complete_generation_time <=> y.latest_complete_generation_time }
        logger.info "RETURNING CONSTANTLY PROJECT"
        return constantly_projects[0]
      end
      logger.info "RETURNING NIL PROJECT"
      
      ## this is really odd, somehow all the constantly projects are generating inactive or deleted
      return nil
      
  end
  
  ## returns a saved generator with the next project that should be generated
  def schedule_next(generator, logger)
    synchronize do
      project = Scheduler.instance.better_next_project(logger)
      if !project.nil?
        logger.info "Assigning Generation of project" + project.name
        generation = Generation.new
        generation.project = project
        generation.generator = generator
        generation.start_time = Time.now
        generation.save
        
        ## make sure the latest info is at the site
        project.publish_site
        
        return generation
      end
      logger.info "Scheduler.instance.better_next_project found NO PROJECTS"
      ## there was no project. return nil
      return nil
    end
    
    
  end
  
  

  
end
